<template>
	<xbd-pages-content title="环境监测">
		<template #content>
			<view class="environmental-monitoring">
				<view class="header">
					<view class="local">{{ address.city || '-' }}</view>
					<view class="weather-type">{{ today? today.text:'' | weatherInfoFormat }}</view>
					<view class="temperature">{{ temperature.avg !== undefined ? temperature.avg : '-' }}℃</view>
					<view class="temperature-max-min">最高 {{ temperature.max !== undefined ? temperature.max : '-' }}℃&nbsp;&nbsp;&nbsp;最低 {{ temperature.min !== undefined ? temperature.min : '-' }}℃</view>
				</view>
				<view class="weather-content" v-if="list.length">
					<view class="weather-item" v-for="(item, index) in list" :key="index">
						<view class="day">{{ item.time | dayFormat }}</view>
						<image class="img" :src="item.code | weatherFormat" mode=""></image>
						<view class="time">{{ item.time | timeFormat('MM-DD') }}</view>
					</view>
				</view>
				<view class="card">
					<view class="air-environment-title">
						生产区环境
					</view>
					<view class="air-environment">
						<view class="air-environment-item" v-for="(item, index) in airInfo" :key="index">
							<view :class="['air-environment-item-top', productAbnormal[index] ? 'normal' : 'abnormal']">
								<view :class="[String(product[item.key]).length === 4 ? 'small' : 'num']">
									{{ product[item.key] !== undefined ? product[item.key] : '-' }}
								</view>
								<view class="title">
									{{ item.title }}
								</view>
							</view>
							<view class="air-environment-item-bottom" :style="productAbnormal[index] ? 'border-bottom: 20rpx solid #FF7543' : 'border-bottom: 20rpx solid #0062CC' + ';'"></view>
						</view>
					</view>
				</view>
				<view class="card">
					<view class="air-environment-title">
						生活区环境
					</view>
					<view class="air-environment">
						<view class="air-environment-item" v-for="(item, index) in airInfo" :key="index">
							<view :class="['air-environment-item-top', lifeAbnormal[index] ? 'normal' : 'abnormal']">
								<view :class="[String(life[item.key]).length === 4 ? 'small' : 'num']">
									{{ life[item.key] !== undefined ? life[item.key] : '-' }}
								</view>
								<view class="title">
									{{ item.title }}
								</view>
							</view> 
							<view class="air-environment-item-bottom" :style="lifeAbnormal[index] ? 'border-bottom: 20rpx solid #FF7543' : 'border-bottom: 20rpx solid #0062CC' + ';'"></view>
						</view>
					</view>
				</view>
			</view>
		</template>
	</xbd-pages-content>
</template>

<script>
	import { Weather } from '@/api/index.js';
	import { mapState } from 'vuex';
	export default {
		data() {
			return {
				today: {}, //当天天气信息信息
				life: {}, // 生活区
				product: {}, // 生产区
				temperature: {}, // 温度
				list: [], // 近一周天气信息
				airInfo: [ // 空气环境循环列表
					{
						title: '温度',
						key: 'temperature'
					},{
						title: '湿度',
						key: 'humidity'
					},{
						title: '风速',
						key: 'speed'
					},{
						title: '风向',
						key: 'direction'
					},{
						title: '气压',
						key: 'pressure'
					},{
						title: '雨量',
						key: 'rainfall'
					},{
						title: '噪音',
						key: 'noise'
					},{
						title: '粉尘',
						key: 'dust'
					}
				]
			};
		},
		computed: {
			...mapState(['address']),
			lifeAbnormal() {
				const arr = [];
				this.airInfo.forEach((item, index) => {
					if (item.title === '温度') {
						(this.life[item.key] <= -10 || this.life[item.key] >= 35) ? arr[0] = true : arr[0] = false;
					} else if (item.title === '湿度') {
						(this.life[item.key] <= 30 || this.life[item.key] >= 70) ? arr[1] = true : arr[1] = false;
					} else if (item.title === '风速') {
						this.life[item.key] >= 10 ? arr[2] = true : arr[2] = false;
					} else if (item.title === '风向') {
						arr[3] = false;
					} else if (item.title === '气压') {
						arr[4] = false;
					} else if (item.title === '雨量') {
						this.life[item.key] >= 50 ? arr[5] = true : arr[5] = false;
					} else if (item.title === '噪音') {
						this.life[item.key] >= 60 ? arr[6] = true : arr[6] = false;
					} else if (item.title === '粉尘') {
						this.life[item.key] >= 150 ? arr[7] = true : arr[7] = false;
					}
				})
				return arr;
			},
			productAbnormal() {
				const arr = [];
				this.airInfo.forEach((item, index) => {
					if (item.title === '温度') {
						(this.product[item.key] <= -10 || this.product[item.key] >= 35) ? arr[0] = true : arr[0] = false;
					} else if (item.title === '湿度') {
						(this.product[item.key] <= 30 || this.product[item.key] >= 70) ? arr[1] = true : arr[1] = false;
					} else if (item.title === '风速') {
						this.product[item.key] >= 10 ? arr[2] = true : arr[2] = false;
					} else if (item.title === '风向') {
						arr[3] = false;
					} else if (item.title === '气压') {
						arr[4] = false;
					} else if (item.title === '雨量') {
						this.product[item.key] >= 50 ? arr[5] = true : arr[5] = false;
					} else if (item.title === '噪音') {
						this.product[item.key] >= 60 ? arr[6] = true : arr[6] = false;
					} else if (item.title === '粉尘') {
						this.product[item.key] >= 150 ? arr[7] = true : arr[7] = false;
					}
				})
				return arr;
			}
		},
		methods: {
			async getWeather() {
				try {
					this.$showLoading('加载中');
					const res = await Weather.getWeatherDetail(this.address);
					uni.hideLoading();
					if(res.data.weather.forecast){
						this.today = res.data.weather.today;
						this.temperature = res.data.weather.today.temperature;
						res.data.weather.forecast.shift();
						this.list = res.data.weather.forecast;
					}
					this.product = res.data.zone.product;
					this.life = res.data.zone.life;
				} catch(e) {
				}
			}
		},
		onLoad() {
			this.getWeather();
		}
	}
</script>

<style lang="scss" scoped>
	.abnormal {
		background-image: url(../../static/images/weather/radius_blue.png);
	}
	.normal {
		background-image: url(../../static/images/weather/radius_red.png);
	}
	.environmental-monitoring {
		.header {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: space-between;
			width: 750rpx;
			height: 142px;
			padding: 20px 0;
			
			font-size: 12px;
			
			background: url(../../static/images/common/environmental.png) no-repeat;
			background-size: 100% 100%;
			color: #FFFFFF;
			
			box-sizing: border-box;
			.local {
				font-size: 16px;
			}
			.temperature {
				font-size: 20px;
			}
		}
		.weather-content {
			display: flex;
			justify-content: space-between;
			align-items: center;
			
			width: 750rpx;
			height: 100px;
			padding: 20px 50rpx;
			margin-bottom: 5px;
			
			background-color: #FFFFFF;
			
			box-sizing: border-box;
			.weather-item {
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				align-items: center;
				height: 100%;
				
				font-size: 12px;
				
				color: $uni-text-color;
				.img {
					width: 34rpx;
					height: 34rpx;
				}
				.time {
					color: #999999;
				}
			}
		}
		.card {
			padding: 20px 50rpx 0;
			margin-bottom: 10px;
			
			background-color: #FFFFFF;
			.air-environment-title {
				margin-bottom: 20px;
				
				font-size: 16px;
				
				color: #555555;
			}
		}
		.air-environment {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			align-content: center;
			&-item {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-bottom: 20px;
				&-top {
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					align-items: center;
					
					width: 140rpx;
					height: 140rpx;
					padding: 15px 0;
					
					border-radius: 50%;
					
					background-size: 100% 100%;
					background-repeat: no-repeat;
					
					box-sizing: border-box;
					color: #FFFFFF;
					
					font-size: 24rpx;
					.num {
						font-size: 40rpx;
					}
					.small {
						font-size: 32rpx;
					}
				}
				&-bottom {
					width: 0;
					height: 0;
					border-top: 20rpx solid transparent;
					border-left: 15rpx solid transparent;
					border-right: 15rpx solid transparent;
				}
			}
		}
	}
</style>
